using System;
using System.Drawing;
using System.Text;
using ICSharpCode.TextEditor.Document;

namespace ICSharpCode.TextEditor
{
    public class HtmlWriter
    {
        public static string GenerateHtml(TextArea textArea)
        {
            return BuildDocument(textArea);
        }

        private static string BuildDocument(TextArea textArea)
        {
            int num = checked((int)textArea.Font.Size);
            int fontHeight = textArea.TextView.FontHeight;
            string themeName = textArea.Document.ThemeName;
            themeName = ((!(themeName == "Black")) ? "#ffffff" : "#1E1E1E");
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append("<div style=\"padding:10px; font-size:" + num + "px; background:" + themeName + ";line-height:" + fontHeight + "px;\">");
            stringBuilder.Append(BuildHtmlHeader(textArea).ToString());
            stringBuilder.Append(BuildHtmlBody(textArea).ToString());
            stringBuilder.Append("</div>");
            string text = stringBuilder.ToString();
            Console.WriteLine("htmlwrite:" + text);
            return text;
        }

        private static string BuildHtmlHeader(TextArea textArea)
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append("<style type=\"text/css\">");
            stringBuilder.Append(".winc-Paragraph{padding:0px;}");
            stringBuilder.Append("</style>");
            return stringBuilder.ToString();
        }

        private static string BuildHtmlBody(TextArea textArea)
        {
            StringBuilder stringBuilder = new StringBuilder();
            foreach (ISelection item in textArea.SelectionManager.SelectionCollection)
            {
                if (!textArea.SelectionManager.IsMutilSelect && !item.IsCurrent)
                {
                    continue;
                }
                for (int i = item.StartPosition.Y; i <= item.EndPosition.Y; i = checked(i + 1))
                {
                    LineSegment lineSegment = textArea.Document.GetLineSegment(i);
                    if (lineSegment.Words == null)
                    {
                        continue;
                    }
                    int offset = lineSegment.Offset;
                    stringBuilder.Append("<div class=\"winc-Paragraph\">");
                    foreach (TextWord word2 in lineSegment.Words)
                    {
                        switch (word2.Type)
                        {
                            case TextWordType.Space:
                                if (item.ContainsOffset(offset))
                                {
                                    stringBuilder.Append("&nbsp;");
                                }
                                break;
                            case TextWordType.Tab:
                                if (item.ContainsOffset(offset))
                                {
                                    stringBuilder.Append("&nbsp;&nbsp;");
                                }
                                break;
                            case TextWordType.Word:
                                {
                                    Color color = word2.Color;
                                    StringBuilder stringBuilder2 = new StringBuilder();
                                    string text = string.Concat(string.Concat("#" + color.R.ToString("X2"), color.G.ToString("X2")), color.B.ToString("X2"));
                                    stringBuilder2.Append("color:" + text + ";");
                                    if (word2.Italic)
                                    {
                                        stringBuilder2.Append("font-style:italic;");
                                    }
                                    if (word2.Bold)
                                    {
                                        stringBuilder2.Append("font-weight:bold;");
                                    }
                                    string word = word2.Word;
                                    word = word.Replace("&", "&amp;");
                                    word = word.Replace("\"", "&quot;");
                                    word = word.Replace("<", "&lt;");
                                    word = word.Replace(">", "&gt;");
                                    string text2 = stringBuilder2.ToString();
                                    stringBuilder.Append("<span style=\"" + text2 + "\">" + word + "</span>");
                                    break;
                                }
                        }
                    }
                    stringBuilder.Append("</div>");
                }
            }
            return stringBuilder.ToString();
        }
    }
}
